from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD

#异或数据集
x_data = [[0., 0.],
          [0., 1.],
          [1., 0.],
          [1., 1.]]
y_data = [[0.],
          [1.],
          [1.],
          [0.]]

#构建模型
model = Sequential()
model.add(Dense(2, input_dim=2, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.1)
#模型配置编译compile
model.compile(loss='binary_crossentropy', optimizer=sgd,
              metrics=['accuracy'])

model.summary()
model.fit(x_data, y_data, epochs=100)

#预测类别
print(model.predict_classes(x_data))

#model.evaluate评估，返回的是x_data,y_data数据集的loss,accuracy
evaluate = model.evaluate(x_data, y_data, verbose=0)
print('Test loss:', evaluate[0])
print('Test accuracy:', evaluate[1])
